#!/usr/bin/env tclsh

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#              _                              #
#             | |                             #
#             | |     _   _  __ _             #
#             | |    | | | |/ _` |            #
#             | |____| |_| | (_| |            #
#             |______|\__,_|\__,_|            #
#                                             #
#             Version   : 0.1                 #
#             Auteur    : Diogene             #
#             IRCdaemon : InspIRCd 2.x        #
#                                             #
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

set ::lua_version 0.1

namespace eval lua {
	set service(dir_root) [file dirname [info script]]
	set service(dir_libs) [file join $service(dir_root) "../libs"]
	set service(authors)  "Diogene"
	lappend ::auto_path $service(dir_libs)
	namespace export getObj SqlConnect SqlDeconnect SqlAsk SqlAct irc*
}

proc lua::Init {} {
    puts "----------------------------------------------"
	puts "               Lua IRC Service                "
	puts "----------------------------------------------"
	puts "Lua version : $::lua_version"
	puts "TCL version : $::tcl_version"
	puts "----------------------------------------------"
	loadPackages
	loadFiles
	irc::c_link
	puts "----------------------------------------------"
}

proc lua::loadPackages {} {
	package require md5
	package require mysqltcl
	package require dns
	package require otp
}

proc lua::loadFiles {} {
	source "config.tcl"
	source "src/scanner.tcl"
	source "src/irc.tcl"
}

proc lua::setCfg { index value } {
	variable cfg
	set cfg($index) $value
}

proc lua::getObj { objType objIndex } {
	variable cfg
	variable service
	if {$objType == "config"} {
		if { [info exists cfg($objIndex)] } {
			return $cfg($objIndex)
		} else {return }
	} elseif {$objType == "var"} {
		if { [info exists service($objIndex)] } {
			return $service($objIndex)
		} else {return }
	} else {
    return
  }
}

proc lua::utimer {time data} {
	set time [expr $time*1000]
	set timer [after $time $data]
	return $timer
}

proc lua::SqlConnect {} {
	set ::mysqlink [mysqlconnect -host [getObj config SQL_HOST] -user [getObj config SQL_LOGIN] -password [getObj config SQL_PASS] -sock [getObj config SQL_SOCK]]
	mysqluse $::mysqlink [getObj config SQL_BASE]
}

proc lua::SqlDeconnect {} {
	mysqlclose $::mysqlink; unset -nocomplain ::mysqlink
}

proc lua::SqlAsk {val args} {
	SqlConnect
	set value1 [lindex $args 0]
	set value2 [lindex $args 1]
	switch -exact $val {
		"apIsTrusted" {
			set res [mysqlsel $::mysqlink "SELECT * FROM LUA_AP_TRUSTED WHERE addr = '$value1'"]
			SqlDeconnect
			return $res
		}
		"apIsHostListed" {
			set res [mysqlsel $::mysqlink "SELECT * FROM LUA_AP_HOSTLIST WHERE addr = '$value1'"]
			SqlDeconnect
			return $res
		}
		"apIsPortListed" {
			set res [mysqlsel $::mysqlink "SELECT * FROM LUA_AP_PORTLIST WHERE addr = '$value1'"]
			SqlDeconnect
			return $res
		}
	}
}

proc lua::SqlAct {val args} {
	SqlConnect
	set value1 [lindex $args 0]
	set value2 [lindex $args 1]
	switch -exact $val {
		"apInsertBL" {
			mysqlsel $::mysqlink "INSERT INTO LUA_AP_HOSTLIST VALUES ('', '$value1')"
		}
	}
	SqlDeconnect
}

lua::Init

vwait forever